package smartclass.utils;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Helper {
	public static int getListViewSize(ListView myListView) {
		ListAdapter myListAdapter = myListView.getAdapter();
		if (myListAdapter == null) {
			// do nothing return null
			return 0;
		}
		// set listAdapter in loop for getting final size
		int totalHeight = 0;
		for (int size = 0; size < myListAdapter.getCount(); size++) {
			View listItem = myListAdapter.getView(size, null, myListView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		// setting listview item in adapter
		ViewGroup.LayoutParams params = myListView.getLayoutParams();
		params.height = totalHeight
				+ (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
		myListView.setLayoutParams(params);
		// print height of adapter on log
		Log.i("height of listItem:", String.valueOf(totalHeight));
		return totalHeight;
	}

	public static int groupExpand(BaseExpandableListAdapter expandListAdapter,
			ExpandableListView expandList, int groupPosition, int totalHeight) {

		if (expandList.getChildCount() == 0) {
			return totalHeight;
		}
		for (int size = 0; size < expandListAdapter
				.getChildrenCount(groupPosition); size++) {
			View listItem = expandListAdapter.getChildView(groupPosition, size,
					true, null, expandList);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = expandList.getLayoutParams();
		params.height = totalHeight
				+ (expandList.getDividerHeight() * (expandList.getCount() - 1));

		expandList.setLayoutParams(params);
		return totalHeight;
	}

	public static int groupCollapse(
			BaseExpandableListAdapter expandListAdapter,
			ExpandableListView expandList, int groupPosition, int totalHeight) {
		if (expandList.getChildCount() == 0) {
			return totalHeight;
		}
		for (int size = 0; size < expandListAdapter
				.getChildrenCount(groupPosition); size++) {
			View listItem = expandListAdapter.getChildView(groupPosition, size,
					true, null, expandList);
			listItem.measure(0, 0);
			totalHeight -= listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = expandList.getLayoutParams();
		params.height = totalHeight
				+ (expandList.getDividerHeight() * (expandList.getCount() - 1));

		expandList.setLayoutParams(params);
		return totalHeight;
	}

	public static int addNewItemgetSize(ExpandableListView expandList,
			int totalHeight, View childView) {
		childView.measure(0, 0);
		totalHeight += childView.getMeasuredHeight();
		ViewGroup.LayoutParams params = expandList.getLayoutParams();
		params.height = totalHeight
				+ (expandList.getDividerHeight() * (expandList.getCount() - 1));

		expandList.setLayoutParams(params);
		return totalHeight;
	}

	public static int removeItemgetSize(ExpandableListView expandList,
			int totalHeight, View childView) {
		childView.measure(0, 0);
		totalHeight -= childView.getMeasuredHeight();
		ViewGroup.LayoutParams params = expandList.getLayoutParams();
		params.height = totalHeight
				+ (expandList.getDividerHeight() * (expandList.getCount() - 1));

		expandList.setLayoutParams(params);
		return totalHeight;
	}
}
